﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Icinetic.FeedCategorySample.Entities;
using Icinetic.FeedCategorySample.Services.Providers;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;

namespace Icinetic.FeedCategorySample.Services.Test
{
    [TestClass]
    public class FeedsProviderTest
    {
        [TestMethod]
        public void GetCacheExpirationGreaterThanNowTest()
        {
            DateTime now = DateTime.Now;
            FeedsProvider feedsProvider = new FeedsProvider();
            DateTime cacheExpiration = feedsProvider.GetCacheExpiration();
            Assert.IsTrue(cacheExpiration > now);
        }
        
        [TestMethod]
        public async Task GetFeedReturnFeedItemsTest()
        {
            FeedsProvider feedsProvider = new FeedsProvider("testfeed.xml");
            List<RssItem> itemList = await feedsProvider.Get<RssItem>(null);
            Assert.IsTrue(itemList.Count > 0);
        }

        [TestMethod]
        public async Task GetFeedPagedTest()
        {
            FeedsProvider feedsProvider = new FeedsProvider("testfeed.xml");
            List<RssItem> itemList = await feedsProvider.Get<RssItem>(null, 5, 1);
            Assert.AreEqual(itemList.Count, 5);
        }

        [TestMethod]
        public async Task GetFeedWithCategoryOverridedTest()
        {
            FeedsProvider feedsProvider = new FeedsProvider("testfeed.xml");
            List<RssItem> itemList = await feedsProvider.Get<RssItem>(null, "TestCategory");
            Assert.IsFalse(itemList.Any(item => item.Category != "TestCategory"));
        }

        [TestMethod]
        public async Task GetFeedWithoutFeedPathTest()
        {
            try
            {
                FeedsProvider feedsProvider = new FeedsProvider();
                List<RssItem> itemList = await feedsProvider.Get<RssItem>(null);
                Assert.Fail("No exception thrown");
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is FileNotFoundException);
            }
        }
    }
}
